import {
    EntityComponentTypes,
    EquipmentSlot,
    ItemLockMode,
    ItemStack,
} from "@minecraft/server";
import { GameComponent } from "@sapi-game/main";
import { HideAndSeekMainState } from "../mainState";
import { HideAndSeekPlayer, HideAndSeekRole } from "../player";

export class SeekerEquipment extends GameComponent<HideAndSeekMainState> {
    override onAttach(): void {}

    loadEquipForAll() {
        this.context.players.forEach((p) => {
            if (p.role == HideAndSeekRole.Seeker) {
                this.loadEquipment(p);
            }
        });
    }

    loadEquipment(player: HideAndSeekPlayer) {
        if (player.role != HideAndSeekRole.Seeker || !player.player) return;
        const equip = player.player.getComponent(
            EntityComponentTypes.Equippable
        );
        if (!equip) return;
        player.clear();
        const chest = new ItemStack("iron_chestplate");
        chest.lockMode = ItemLockMode.slot;
        const leggings = new ItemStack("iron_leggings");
        leggings.lockMode = ItemLockMode.slot;
        const boots = new ItemStack("iron_boots");
        boots.lockMode = ItemLockMode.slot;

        equip.setEquipment(EquipmentSlot.Chest, chest);
        equip.setEquipment(EquipmentSlot.Legs, leggings);
        equip.setEquipment(EquipmentSlot.Feet, boots);
        player.giveItem(new ItemStack("iron_sword"));
    }
}
